configWeixin();
changePx(); //改变根元素的px
var getSearchObj = getSerch(location.search);
var activityDTO;
getBaseInfor();
function getnumberlsit(){
	$.ajax({
		type: 'POST',
		contentType: "application/json;charset=UTF-8",
        url: 'http://api.ucmd.cn/renren-api/act/activity/statistics',
        dataType: 'json',
        data:JSON.stringify({
		  "openId": getSearchObj.openId,
		  "quantity": 1,
		  "type": 0  //0:浏览 1:分享 2:购买
		}),
        success: function(data){
        	if(data.data){
        		console.log(data.data)
        	}
        }
	})
}
function getBaseInfor(){
    $.ajax({
        type: 'GET',
        url: 'http://api.ucmd.cn/renren-api/act/activity/getActivity',
        dataType: 'json',
        success: function(data){
            if(data.code==0){
                var data = data.data.activityDTO;
				activityDTO = data;
				shareListener();
                var html=template('test',data);
				document.title = data.title
                document.getElementById('content').innerHTML = html;
                $('#audio').attr("src",data.activityMusicUrl)
                //收集浏览量、分享量、购买量数据
                getnumberlsit()
                //品牌介绍
                var imgArr= data.introduce.split(","),str="";
                imgArr.forEach(function(v,i){
                    str+=`<img src='${v}'/>`
                })
                $('.brand').html(str);
                //音乐
                audioAutoPlay('audio');
                //人物头像跳动
                PicBeat();
                //前往分校
                $(".goToShop").click(function(){
                    var _index=$(this).data('count')
                    $(".campusMiain").hide();
                    $('.ToShopMain'+_index).show();
                })
                //切换分校
                $(".switch").click(function(){
                    $(".campusMiain").show();
                    $(".shopPart").hide();
                })
                //显示详情
                $(".courseDir").click(function(){
                    var $img=$(this).data('img')
                    $(".piro_overlay").show().find('img').attr('src',$img);
                })
                //关闭弹窗
                $(".closePop").click(function(){
                    $("body").css("overflow","auto");
                    $(".fixed_show").hide()
                })
                //前去购买
                $(".shopBtn").click(function(){
                    var curTime = new Date();
                    var endtime = new Date($(".activeend").text().replace(/\-/g, "\/"));
                    var payPrice=$(this).data('price'),payName=$(this).data('name'),goodsId=$(this).data('id');
                    if(curTime<=endtime){
                        location.href='register.html?openId='+getSearchObj.openId+'&payPrice='+payPrice+'&payName='+payName+'&firstOpenId='+getSearchObj.firstOpenId+'&parentOpenId='+getSearchObj.parentOpenId+'&goodsId='+goodsId
                    }else{
                        swal({
                            title: "温馨提示",
                            text: "活动已结束",
                            icon: "warning",
                            button: "确认",
                            dangerMode: true
                        })
                    }
                })

            }
        },
        error: function(xhr, type){
            //swal('Ajax error!');
        }
    });
}
//人物头像跳动
function PicBeat(){
    var num=0;
    var $len=$(".actorList li").length-2;
    setInterval(function(){
        $(".bounce").removeClass('bounce');
        if(num>$len){
            num=0;
        }else{
            $(".actorList li").eq(num).addClass('bounce')
            num++
        }
    },1100)
}

function shareListener(){
// 	wx.ready(function () {   //需在用户可能点击分享按钮前就先调用
// 		wx.updateAppMessageShareData({ 
// 			title: activityDTO.shareTitle, // 分享标题
// 			desc: activityDTO.shareContent,
// 			link: "http://api.ucmd.cn/renren-api/wx/activity/wxc57d0b29494417fe/oauth?state=1&parentOpenId="+getSearchObj.parentOpenId+"&firstOpenId="+getSearchObj.firstOpenId, // 分享链接
// 			imgUrl: activityDTO.shareImg, // 分享图标
// 			success: function () {
// 			  // 设置成功
// 			}
// 		})
// 		
// 		wx.updateTimelineShareData({ 
// 			title: activityDTO.shareTitle, // 分享标题
// 			desc: activityDTO.shareContent,
// 			link: "http://api.ucmd.cn/renren-api/wx/activity/wxc57d0b29494417fe/oauth?state=1&parentOpenId="+getSearchObj.parentOpenId+"&firstOpenId="+getSearchObj.firstOpenId, // 分享链接
// 			imgUrl: activityDTO.shareImg, // 分享图标
// 			success: function () {
// 			  // 设置成功
// 			}
// 		})
// 	});
	
	wx.ready(function() {
		wx.onMenuShareTimeline({
			title: activityDTO.shareTitle, // 分享标题
			desc: activityDTO.shareContent,
			link: "http://api.ucmd.cn/renren-api/wx/activity/wxc57d0b29494417fe/oauth?state=1&parentOpenId="+getSearchObj.openId+"&firstOpenId="+getSearchObj.firstOpenId, // 分享链接
			imgUrl: activityDTO.shareImg, // 分享图标
			success: function() {
				$.ajax({
					type: 'POST',
					contentType: "application/json;charset=UTF-8",
				    url: 'http://api.ucmd.cn/renren-api/act/activity/statistics',
				    dataType: 'json',
				    data:JSON.stringify({
					  "openId": getSearchObj.openId,
					  "quantity": 1,
					  "type": 1  //0:浏览 1:分享 2:购买
					}),
				    success: function(data){
				    	if(data.data){
				    		console.log(data.data)
							swal('分享成功!');
							window.history.back();
				    	}
				    }
				})
			},
			cancel: function() {
				alert("失败")
			}
		});
	
		wx.onMenuShareAppMessage({
			title: activityDTO.shareTitle, // 分享标题
			desc: activityDTO.shareContent,
			link: "http://api.ucmd.cn/renren-api/wx/activity/wxc57d0b29494417fe/oauth?state=1&parentOpenId="+getSearchObj.openId+"&firstOpenId="+getSearchObj.firstOpenId, // 分享链接
			imgUrl: activityDTO.shareImg, // 分享图标
			success: function() {
				$.ajax({
					type: 'POST',
					contentType: "application/json;charset=UTF-8",
				    url: 'http://api.ucmd.cn/renren-api/act/activity/statistics',
				    dataType: 'json',
				    data:JSON.stringify({
					  "openId": getSearchObj.openId,
					  "quantity": 1,
					  "type": 1  //0:浏览 1:分享 2:购买
					}),
				    success: function(data){
				    	if(data.data){
				    		console.log(data.data)
							swal('分享成功!');
							window.history.back();
				    	}
				    }
				})
			},
			cancel: function() {
				alert("失败")
			}
		});
		wx.error(function(res){
			// config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
			alert("errorMSG:"+activityDTO.title+JSON.stringify(res)+JSON.stringify(res,msg));
		});
	});
}


$(function(){
    $('#QRCode').attr('data-firstOpenId',getSearchObj.firstOpenId)
    if(undefined != getSearchObj.hasPersonalCenter && getSearchObj.hasPersonalCenter!=2){
        $(".myCharges").attr('href','rebate.html?openId='+getSearchObj.openId+'&hasPersonalCenter='+getSearchObj.hasPersonalCenter).show()
    }
    //动画调用
    loading();
    $('#closeLink').click(function(){
        $('.linkHref').hide()
    })
});

